/**
 * Cornerstone Store
 * 
 * 管理 Cornerstone3D 的全局状态
 * 
 * @author yangzhixin
 * @module stores/cornerstone
 */

import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import type { CornerstoneFacade } from 'js-3d-model';

/**
 * 视口状态接口
 */
export interface ViewportState {
  id: string;
  type: string;
  elementId: string;
  isActive: boolean;
}

/**
 * 工具组状态接口
 */
export interface ToolGroupState {
  id: string;
  viewportIds: string[];
  activeTools: string[];
}

/**
 * Cornerstone Store
 */
export const useCornerstoneStore = defineStore('cornerstone', () => {
  // ========== 状态 ==========
  
  /** Facade 实例 */
  const facade = ref<CornerstoneFacade | null>(null);
  
  /** 是否已初始化 */
  const isInitialized = ref(false);
  
  /** 是否正在初始化 */
  const isInitializing = ref(false);
  
  /** 视口列表 */
  const viewports = ref<ViewportState[]>([]);
  
  /** 工具组列表 */
  const toolGroups = ref<ToolGroupState[]>([]);
  
  /** 当前激活的视口ID */
  const activeViewportId = ref<string | null>(null);
  
  /** 当前激活的工具组ID */
  const activeToolGroupId = ref<string | null>(null);
  
  /** 错误信息 */
  const error = ref<string | null>(null);

  // ========== 计算属性 ==========
  
  /** 当前激活的视口 */
  const activeViewport = computed(() => {
    if (!activeViewportId.value) return null;
    return viewports.value.find((vp) => vp.id === activeViewportId.value);
  });
  
  /** 当前激活的工具组 */
  const activeToolGroup = computed(() => {
    if (!activeToolGroupId.value) return null;
    return toolGroups.value.find((tg) => tg.id === activeToolGroupId.value);
  });
  
  /** 视口数量 */
  const viewportCount = computed(() => viewports.value.length);
  
  /** 工具组数量 */
  const toolGroupCount = computed(() => toolGroups.value.length);

  // ========== 方法 ==========
  
  /**
   * 设置 Facade 实例
   */
  function setFacade(facadeInstance: CornerstoneFacade | null) {
    facade.value = facadeInstance;
    isInitialized.value = !!facadeInstance;
  }
  
  /**
   * 设置初始化状态
   */
  function setInitializing(value: boolean) {
    isInitializing.value = value;
  }
  
  /**
   * 添加视口
   */
  function addViewport(viewport: ViewportState) {
    const existing = viewports.value.find((vp) => vp.id === viewport.id);
    if (!existing) {
      viewports.value.push(viewport);
    }
  }
  
  /**
   * 移除视口
   */
  function removeViewport(viewportId: string) {
    const index = viewports.value.findIndex((vp) => vp.id === viewportId);
    if (index !== -1) {
      viewports.value.splice(index, 1);
    }
    
    // 如果移除的是当前激活的视口，清空激活状态
    if (activeViewportId.value === viewportId) {
      activeViewportId.value = null;
    }
  }
  
  /**
   * 设置激活的视口
   */
  function setActiveViewport(viewportId: string | null) {
    activeViewportId.value = viewportId;
    
    // 更新视口的激活状态
    viewports.value.forEach((vp) => {
      vp.isActive = vp.id === viewportId;
    });
  }
  
  /**
   * 添加工具组
   */
  function addToolGroup(toolGroup: ToolGroupState) {
    const existing = toolGroups.value.find((tg) => tg.id === toolGroup.id);
    if (!existing) {
      toolGroups.value.push(toolGroup);
    }
  }
  
  /**
   * 移除工具组
   */
  function removeToolGroup(toolGroupId: string) {
    const index = toolGroups.value.findIndex((tg) => tg.id === toolGroupId);
    if (index !== -1) {
      toolGroups.value.splice(index, 1);
    }
    
    // 如果移除的是当前激活的工具组，清空激活状态
    if (activeToolGroupId.value === toolGroupId) {
      activeToolGroupId.value = null;
    }
  }
  
  /**
   * 设置激活的工具组
   */
  function setActiveToolGroup(toolGroupId: string | null) {
    activeToolGroupId.value = toolGroupId;
  }
  
  /**
   * 更新工具组的激活工具
   */
  function updateToolGroupActiveTools(toolGroupId: string, activeTools: string[]) {
    const toolGroup = toolGroups.value.find((tg) => tg.id === toolGroupId);
    if (toolGroup) {
      toolGroup.activeTools = activeTools;
    }
  }
  
  /**
   * 设置错误
   */
  function setError(errorMessage: string | null) {
    error.value = errorMessage;
  }
  
  /**
   * 清除错误
   */
  function clearError() {
    error.value = null;
  }
  
  /**
   * 重置状态
   */
  function reset() {
    facade.value = null;
    isInitialized.value = false;
    isInitializing.value = false;
    viewports.value = [];
    toolGroups.value = [];
    activeViewportId.value = null;
    activeToolGroupId.value = null;
    error.value = null;
  }

  return {
    // 状态
    facade,
    isInitialized,
    isInitializing,
    viewports,
    toolGroups,
    activeViewportId,
    activeToolGroupId,
    error,
    
    // 计算属性
    activeViewport,
    activeToolGroup,
    viewportCount,
    toolGroupCount,
    
    // 方法
    setFacade,
    setInitializing,
    addViewport,
    removeViewport,
    setActiveViewport,
    addToolGroup,
    removeToolGroup,
    setActiveToolGroup,
    updateToolGroupActiveTools,
    setError,
    clearError,
    reset,
  };
});


